﻿var _ = require('underscore');

var DatabaseSchemaReader = require('happy.database.schem.reader').DatabaseSchemaReader;
var CodeGenerator = require('happy.code.generator').CodeGenerator;

var project = {
    projectDirectory: 'F:\\学习项目\\规律化学习\\WcfServiceTemplate\\WcfServiceTemplate', //输出目录
    rootNamespace: 'WcfServiceTemplate', //根命名空间
    connectionString: 'Data Source=192.168.0.14\\SQLEXPRESS;Initial Catalog=Tenoner3;Persist Security Info=True;User ID=sa;Password=woshishui2010;', //连接字符串
    openTables: ['HrEmployees'] //要生成的表，也可以全部生成。
};

//和数据库无关的代码生成
CodeGenerator.executeTemplateDirectory('./templates/Bases', project);

//和数据库相关的代码生成
var schemaReader = DatabaseSchemaReader.createSqlClientReader(project.connectionString);
schemaReader.readAll(function (error, schema) {
    if (error) {
        console.log(error)
        return;
    }

    _.each(schema.tables, function (table) {
        if (_.contains(project.openTables, table.name)) {
            var ejsOptions = _.extend({
                table: table,
                baseNamespace: 'Test'
            }, project);

            CodeGenerator.executeTemplateDirectory('./templates/Tables', ejsOptions);
        }
    });

    _.each(schema.views, function (table) {
        if (_.contains(project.openTables, table.name)) {
            var ejsOptions = _.extend({
                table: table,
                baseNamespace: 'Test'
            }, project);

            CodeGenerator.executeTemplateDirectory('./templates/Tables', ejsOptions);
        }
    });

    console.log('success!')
});
